package com.steven.ourcontest.base

import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.Window
import androidx.appcompat.app.AppCompatActivity
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.steven.ourcontest.R
import io.reactivex.disposables.CompositeDisposable


abstract class BaseActivity(private val isImmersed: Boolean = false) : AppCompatActivity() {

    private val statusBarColor: Int = R.color.app_base
    private val statusBarColorString: String? = null
    val disposables: CompositeDisposable = CompositeDisposable()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (!isImmersed) {
            if (getStatusBarColorString() != null) {
                ImmersionBar.with(this).statusBarColor(getStatusBarColorString())
            } else {
                ImmersionBar.with(this).statusBarColor(getStatusBarColor())
            }.autoDarkModeEnable(true).navigationBarEnable(false).init()
        } else {
            ImmersionBar.with(this)
                .fitsSystemWindows(false)
                .transparentStatusBar()
                .hideBar(BarHide.FLAG_HIDE_BAR)
                .init()
        }
        getViewId()?.let { setContentView(it) }
        initView()
        initSmartRefresh()?.let {
            it.setEnableAutoLoadMore(false)
            it.setEnableRefresh(true)
            it.setRefreshFooter(ClassicsFooter(this))
            it.setRefreshHeader(ClassicsHeader(this))
        }
    }

    override fun onDestroy() {
        disposables.clear()
        super.onDestroy()
    }

    open fun getStatusBarColor(): Int {
        return statusBarColor
    }

    open fun getStatusBarColorString(): String? {
        return statusBarColorString
    }

    abstract fun getViewId(): Int?

    abstract fun initView()

    open fun initSmartRefresh(): SmartRefreshLayout? {
        return null
    }
}